#include<bits/stdc++.h>
using namespace std;
struct student {
	string name;
	int year;
	int mon;
	int day;
	int rank;
};
bool cmp(struct student s1, struct student s0)
{
	if (s0.year == s1.year) {
		if (s0.mon == s1.mon) {
			if (s0.day == s1.day) {
				return s0.rank < s1.rank;
			}
			return s0.day > s1.day;
		}
		return s0.mon > s1.mon;
	}
	return s0.year > s1.year;
}
int main()
{
	int n;
	cin >> n;
	student students[n];

	for (int i = 0; i < n; i++) {
		cin >> students[i].name >> students[i].year >> students[i].mon >> students[i].day;
		students[i].rank = i;
	}
	sort(students, students + n, cmp);
	for (int i = 0; i < n; i++) {
		cout << students[i].name ;
		if ( i != n - 1) {
			cout << endl;
		}
	}

	return 0;
}

